package hash;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/8/19
 */
public class TwoNumSum {

    public static void main(String[] args) {
        int[] arr = new int[] {1,3,4,2,5,8,7};
        int target = 10;
        printTwoNumIndex(target,arr);
    }

    private static void printTwoNumIndex(int target,int[] arr) {
        Map<Integer, Integer> hashMap = new HashMap<>(arr.length);
        for (int i = 0; i < arr.length; i++) {
            hashMap.put(arr[i],i);
        }

        for (int i = 0; i < arr.length; i++) {
            int temp = target-arr[i];
            Integer j = hashMap.get(temp);
            if (Objects.nonNull(j)) {
                if (i != j && i<j) {
                    System.out.println("找到符合要求的元素，下标：" + i + "," +j);
                }
            }
        }
    }
}
